package com.ppsea;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 放到160上执行
 * @author smaller
 */
public class JarFxwrTools {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length < 2){
			System.out.println("Sorry,params is wrong. Please less than 2.Type(0:fxwr1:xmy) Id(QQ;UID;PlayerId)");
			return;
		}
		
		int type = Integer.valueOf(args[0]);
		String idTxt = args[1];		//QQ或者UID或者PlayerId
		
		//打印消息Builder
		StringBuilder content = new StringBuilder();
		
		if (type == 0) { // 非仙
			int flag = 0;
			String uid = ""; // openId
			String qq = ""; // QQ
			if (idTxt.length() > 32) {
				System.out.println("OpenId不符合要求的32位格式！");
				return;
			} else if (idTxt.length() == 32) {
				uid = idTxt;
				qq = Utils.Uid2QQ(idTxt);
				flag = 1; // uid
			} else {
				qq = idTxt;
				if (qq == null || StringUtils.isBlank(qq)) {
					System.out.println("\r\n\r\n请输入32位玩家OpenId或者QQ号!");
					return;
				}
				uid = Utils.QQ2Uid(idTxt);
				flag = 2; // qq
				if (uid == null || StringUtils.isBlank(uid)) {
					System.out.println("\r\n\r\n转化QQ成OpenId失败,1:可能因查询比较频繁腾讯返回数据为空2:此QQ号还未注册,请稍候再试!");
					return;
				}
			}
			int hashcode = Math.abs(uid.hashCode()); // 玩家ID对应的hash值
			for (int i = 1; i < 4; i++) {
				String pwd = Utils.queryPlayerPwd(uid, i);
				switch (i) {
				case 1:
					content.append("仙界:\r\n");
					break;
				case 2:
					content.append("魔界:\r\n");
					break;
				case 3:
					content.append("怀旧区:\r\n");
				}
				content.append(Utils.generateFxwrBookMark(uid, pwd, i));
				if (i == 3) {
					content.append("\r\n\r\n");
				} else {
					content.append("\r\n");
				}
			}
			if (flag == 1) {
				content.append("QQ: ").append(qq).append("\r\n\r\n");
			} else if (flag == 2) {
				content.append("UID: ").append(uid).append("\r\n\r\n");
			}
			content.append("仙界app:").append(hashcode % 6).append("\r\n");
			content.append("仙界data:").append(hashcode % 6).append("\r\n");
			content.append("仙界profile:").append(hashcode % 8).append("\r\n\r\n");

			content.append("魔界app:").append(hashcode % 5).append("\r\n");
			content.append("魔界data:").append(hashcode % 3).append("\r\n");
			content.append("魔界profile:").append(hashcode % 2).append("\r\n\r\n");

			content.append("怀旧区app:").append(hashcode % 4).append("\r\n");
			content.append("怀旧区data:").append(hashcode % 3).append("\r\n");
			content.append("怀旧区profile:").append(hashcode % 2).append("\r\n\r\n");

			content.append("xx_data index:").append(hashcode % 10).append("\r\n");
			content.append("xx_data_item index:").append(hashcode % 50);

			System.out.println(content.toString());
			return;
		} else if (type == 1) {
			if(args.length < 3){
				System.out.println("Sorry,XMY need more than 3 params.IdType(1:PlayerId Other:QQ or UID)");
				return;
			}
			int idType = Integer.valueOf(args[2]);	//区分id的类型
			if (idType == 1) {// PlayerId
				try {
					int playerId = Integer.valueOf(idTxt);
					int zoneId = playerId / 1000000;
					JSONObject playerJson = Utils.queryXMYPlayer(String.valueOf(playerId), zoneId);
					if(playerJson == null){
						System.out.println("\r\n\r\n此玩家不存在,请重新输入!");
						return;
					}
					String pwd = playerJson.getString("password").toString();
					switch (zoneId) {
					case 0:
						content.append("玄天界:\r\n");
						break;
					case 1:
						content.append("星海界:\r\n");
						break;
					case 2:
						content.append("苍穹界:\r\n");
					}
					String bookmarkUrl = Utils.generateXmyBookMark(String.valueOf(playerId), pwd, zoneId);
					content.append(bookmarkUrl).append("\r\n\r\n");
					content.append("PLayerId: ").append(playerId).append("\r\n");
					String openId = playerJson.getString("openId");
					String QQ = Utils.Uid2QQ(openId);
					content.append("QQ: ").append(QQ).append("\r\n");
					content.append("OpenId: ").append(openId).append("\r\n");
					content.append("Sex: ").append(Integer.valueOf(playerJson.getString("gender")) == 1 ? "男" : "女").append("\r\n");
					content.append("Name: ").append(playerJson.getString("name")).append("\r\n");
					content.append("Level: ").append(playerJson.getString("level")).append("\r\n");
					content.append("Exp: ").append(playerJson.getString("exp")).append("\r\n");
					content.append("Password: ").append(pwd).append("\r\n");
					content.append("Phone: ").append(playerJson.getString("phoneNum")).append("\r\n");
					content.append("Gold: ").append(playerJson.getString("gold")).append("\r\n");
					content.append("Money: ").append(playerJson.getString("money")).append("\r\n");
					content.append("txXB: ").append(playerJson.getString("txXB")).append("\r\n");
					content.append("txXY: ").append(playerJson.getString("txXY")).append("\r\n\r\n");
				} catch (Exception e2) {
					System.out.println("\r\n\r\nPlayerId输入有误,请重新输入!");
					return;
				}
			} else {
				int flag = 0;
				String uid = ""; // openId
				String qq = ""; // QQ
				if (idTxt.length() > 32) {
					System.out.println("OpenId不符合要求的32位格式！");
				} else if (idTxt.length() == 32) {
					uid = idTxt;
					qq = Utils.Uid2QQ(idTxt);
					flag = 1; // uid
				} else {
					qq = idTxt;
					if (qq == null || StringUtils.isBlank(qq)) {
						System.out.println("\r\n\r\n请输入32位玩家OpenId或者QQ号!");
						return;
					}
					uid = Utils.QQ2Uid(idTxt);
					flag = 2; // qq
					if (uid == null || StringUtils.isBlank(uid)) {
						System.out.println("\r\n\r\n转化QQ成OpenId失败,1:可能因查询比较频繁腾讯返回数据为空2:此QQ号还未注册,请稍候再试!");
						return;
					}
				}
				if (flag == 1) {
					content.append("QQ: ").append(qq).append("\r\n\r\n");
				} else if (flag == 2) {
					content.append("UID: ").append(uid).append("\r\n\r\n");
				}
				for (int i = 0; i <= 2; i++) {// 现在两个界
					switch (i) {
						case 0:
							content.append("玄天界:\r\n");
							break;
						case 1:
							content.append("星海界:\r\n");
							break;
						case 2:
							content.append("苍穹界:\r\n");
					}
					JSONObject playerJson = Utils.queryXMYPlayer(uid, i);
					if(playerJson == null){
						content.append("玩家信息为空,可能没注册此去! ").append("\r\n\r\n");
						continue;
					}
					try {
						String pwd = playerJson.getString("password").toString();
						String playerId = playerJson.getString("id");
						
						String bookmarkUrl = Utils.generateXmyBookMark(playerId, pwd, i);
						content.append(bookmarkUrl).append("\r\n\r\n");
						content.append("PLayerId: ").append(playerId).append("\r\n");
						content.append("Sex: ").append(Integer.valueOf(playerJson.getString("gender")) == 1 ? "男" : "女").append("\r\n");
						content.append("Name: ").append(playerJson.getString("name")).append("\r\n");
						content.append("Level: ").append(playerJson.getString("level")).append("\r\n");
						content.append("Exp: ").append(playerJson.getString("exp")).append("\r\n");
						content.append("Password: ").append(pwd).append("\r\n");
						content.append("Phone: ").append(playerJson.getString("phoneNum")).append("\r\n");
						content.append("Gold: ").append(playerJson.getString("gold")).append("\r\n");
						content.append("Money: ").append(playerJson.getString("money")).append("\r\n");
						content.append("txXB: ").append(playerJson.getString("txXB")).append("\r\n");
						content.append("txXY: ").append(playerJson.getString("txXY")).append("\r\n\r\n");
					} catch (JSONException e) {
						System.out.println("PlayerJson has err:"+e.getMessage());
					}
				}
			}
			System.out.println(content.toString());
			return;
		}
	}
}